/**
 * 财务科目管理初始化
 */
var Fee = {
    id: "FeeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Fee.initColumn = function () {
    return [
            {field: 'selectItem', radio: true},
            {title: '收支ID', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '名称', field: 'fee_name', visible: true, align: 'center', valign: 'middle'},
            {title: '类型', field: 'fee_type', visible: true, align: 'center', valign: 'middle',
                formatter:function(value,row,index){
                if(row.fee_type==0){
                    return "系统";
                }else if(row.fee_type==1){
                    return "自定义"
                }else{
                    return "";
                }
            } },
            {title: '事务对象', field: 'use_object', visible: true, align: 'center', valign: 'middle',
                formatter:function(value,row,index){
                //1本地事务 2 上游网点 3 收货人 4 中转网点
               var result='';
               var type= row.use_object.split(',');
               for(var i=0;i<type.length;i++){
                    if(type[i]==1){
                        result+="本地事务,";
                    }else if(type[i]==2){
                        result+="上游网点,";
                    }else if(type[i]==3){
                        result+="收货人,";
                    }else if(type[i]==4){
                        result+="中转网点,";
                    }else if(type[i]==5){
                        result+="司机,";
                    }
               }
                    result= (result.substring(result.length-1)==',')?result.substring(0,result.length-1):result;
                   return result;
            }},
            {title: '收支分类', field: 'payments_type', visible: true, align: 'center', valign: 'middle',
                formatter:function(value,row,index){
                if(row.payments_type==1){
                    return "收入";
                }else if(row.payments_type==2){
                    return "支出"
                }else{
                    return "";
                }
            } },
            {title: '运单调用', field: 'is_waybill', visible: true, align: 'center', valign: 'middle',
                formatter:function(value,row,index){
                    if(row.is_waybill==0){
                        return "否";
                    }else if(row.is_waybill==1){
                        return "是"
                    }else{
                        return "";
                    }
                }},
            {title: '创建时间', field: 'create_time', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'is_use', visible: true, align: 'center', valign: 'middle',
                formatter:function(value,row,index){
                if(row.is_use==0){
                    return "启用";
                }else if(row.is_use==1){
                    return "未启用"
                }else{
                    return "";
                }
            }},
            {title: '备注', field: 'mark', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Fee.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }if(selected[0].fee_type==0){
        Feng.info("系统类型不能修改！");
        return false;
    } else{
        Fee.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加财务科目
 */
Fee.openAddFee = function () {
    var index = layer.open({
        type: 2,
        shade: 0.01,
        title: '添加财务科目',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/fee/fee_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看财务科目详情
 */
Fee.openFeeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            shade: 0.01,
            title: '财务科目详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/fee/fee_update/' + Fee.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除财务科目
 */
Fee.delete = function () {
    if (this.check()) {
        var operation = function(){
            var feeId = Fee.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/fee/delete", function (data) {
                Feng.success("删除成功!");
                Fee.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("feeId", feeId);
            ajax.start();
        };
        Feng.confirm("是否删除财务科目?",operation);
    }
};

/**
 * 查询财务科目列表
 */
Fee.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Fee.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Fee.initColumn();
    var table = new BSTable(Fee.id, "/fee/list", defaultColunms);
    Fee.table = table.init();
    layui.use('layer', function () {

    })
});
